package com.ff.neuropp.controller;


import com.ff.neuropp.domain.Coupon;
import com.ff.neuropp.service.CouponService;
import com.ff.neuropp.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券管理接口
 */
@RestController
@RequestMapping("/api/coupon")
@Api(value = "优惠券管理controller", tags = {"优惠券管理接口"})
public class CouponController {

    @Resource
    private CouponService couponService;

    @PostMapping("/create")
//    @RequiredPermission("/api/coupon/create")
    @ApiOperation(value = "保存信息",notes = "保存信息")
    public Map<String,Object> saveCoupon(@RequestBody Coupon coupon){
        Coupon resultCoupon = couponService.saveCoupon(coupon);
        return ResultTool.genSuccessResult("success",resultCoupon);
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询信息(PC)", notes = "根据id查询信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Coupon dCoupon = couponService.findById(id);
        if (null == dCoupon) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        return ResultTool.genSuccessResult("success",dCoupon);
    }

    @PostMapping("/update")
//    @RequiredPermission("/api/coupon/update")
    @ApiOperation(value = "修改信息", notes = "修改信息")
    public Map<String,Object> updateCoupon(@RequestBody Coupon coupon){
        Coupon dCoupon = couponService.findById(coupon.getId());
        if(null == dCoupon){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        Coupon uCoupon = couponService.saveCoupon(coupon);
        return ResultTool.genSuccessResult("success",uCoupon);
    }

    @PostMapping("/status")
//    @RequiredPermission("/api/coupon/status")
    @ApiOperation(value = "修改状态 1：上线、0：下线", notes = "修改状态 1：上线、0：下线")
    public Map<String,Object> updateCoupon(@ApiParam(required = true,value = "编号id") @RequestParam Integer id,
                                                   @ApiParam(required = true,value = "状态") @RequestParam Integer status){
        Coupon dCoupon = couponService.findById(id);
        if(null == dCoupon){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        dCoupon.setStatus(status);
        Coupon uCoupon = couponService.saveCoupon(dCoupon);
        return ResultTool.genSuccessResult("success",uCoupon);
    }

    @PostMapping("/delete")
//    @RequiredPermission("/api/coupon/delete")
    @ApiOperation(value = "删除信息", notes = "删除优惠券信息")
    public  Map<String,Object> deleteCoupon(@ApiParam(required = true,value = "id") @RequestParam Integer id){
        Coupon dCoupon = couponService.findById(id);
        if (null == dCoupon) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        couponService.deleteById(id);
        return ResultTool.genSuccessResult("success");

    }
    @GetMapping("/list")
    @ApiOperation(value = "获取所有信息", notes = "获取所有优惠券信息")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String name,
                                        HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        if (null != name && name.length() > 0) {
            param.put("LIKE_name", name);
        }
        List<Coupon> list = couponService.findList(param);
        Map<String, Object> result = new HashMap<>();
        if (null != list && list.size() > 0) {
            result.put("total",list.size());
            result.put("list",list);
            return ResultTool.genSuccessResult("success",result);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
    }

    @GetMapping("/listpage")
//    @RequiredPermission("/api/coupon/listpage")
    @ApiOperation(value = "信息分页", notes = "优惠券信息分页")
    public Map<String, Object> findListPage(@Valid Coupon coupon,
                                            HttpServletRequest request) {

        Page<Coupon> list = couponService.findPage(coupon);
        if(null !=list && list.getTotalElements()>0){
            return ResultTool.genSuccessResult4Page("success",list);
        }else{
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }

    }
}
